package com.jumi.microservice.stock.service.impl;

import com.jumi.microservice.common.exception.ExceptionSupply;
import com.jumi.microservice.domain.CommodityRenderApplyDTO;
import com.jumi.microservice.service.InventoryApi;
import com.jumi.microservice.stock.domain.CommodityRenderVo;
import com.jumi.microservice.stock.domain.StockOccupy4CommodityDTO;
import com.jumi.microservice.stock.dto.commodity.*;
import com.jumi.microservice.stock.mapper.CommodityApplyMapper;
import com.jumi.microservice.stock.service.CommodityApplyService;
import com.jumi.microservice.stock.service.StockService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ssk
 * @create 2020/9/1 10:06
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class CommodityApplyServiceImpl implements CommodityApplyService {

    @Resource
    CommodityApplyMapper commodityApplyMapper;

    @Resource
    StockService stockService;


    @DubboReference
    InventoryApi inventoryApi;


    @Override
    @Transactional
    public int save(CommodityApplyDTO applyRequest) {

        try {
            int rows = commodityApplyMapper.save(applyRequest);
            Long applyId = applyRequest.getId();
            List<CommodityApplyDetailDTO> details = applyRequest.getDetails();
            for (CommodityApplyDetailDTO detail : details) {
                detail.setApplyId(applyId);
            }
            List<CommodityApplyMaterialDTO> materials = applyRequest.getMaterials();
            for (CommodityApplyMaterialDTO material : materials) {
                material.setApplyId(applyId);
            }
            commodityApplyMapper.saveDetails(details);
            commodityApplyMapper.saveMaterials(materials);
            return rows;

        } catch (Exception e) {
            throw new ExceptionSupply(500, "参数不正确").newException();
        }
    }

    @Override
    public int update(CommodityApplyDTO applyRequest) {
        return 0;
    }

    @Override
    public List<CommodityApplyDTO> findByPage(CommodityApplyDTO applyDTO) {
        List<CommodityApplyDTO> list = commodityApplyMapper.findAll(applyDTO);
        return list;
    }

    @Override
    @Transactional
    public CommodityApplyDTO findById(Long id, Integer type) {
        CommodityApplyDTO applyDTO = commodityApplyMapper.findById(id);
        List<CommodityApplyDetailDTO> applyDetails = applyDTO.getDetails();
        int preStockTotalNum = 0;
        for (CommodityApplyDetailDTO detail : applyDetails) {
            preStockTotalNum += detail.getLeftPreNum();
        }
        if (preStockTotalNum > applyDTO.getPreStockTotalNum())
            throw new ExceptionSupply(500, "预售数量分配出现异常").newException();
        applyDTO.setPreStockTotalNum(preStockTotalNum);
        List<CommodityApplyMaterialDTO> materials = applyDTO.getMaterials();

        if (type != null) {
            List<CommodityRenderResultDTO> renders = commodityApplyMapper.findRendersByType(id, type);

            for (CommodityRenderResultDTO render : renders) {
                List<CommodityRenderDTO> details = commodityApplyMapper.findRenders(applyDTO.getId(), render.getWarehouseId());
                List<CommodityApplyDetailDTO> tempList = new ArrayList<>();
                for (CommodityRenderDTO detail : details) {
                    for (CommodityApplyDetailDTO applyDetail : applyDetails) {
                        if (detail.getSkuId().equals(applyDetail.getSkuId())) {
                            CommodityApplyDetailDTO temp = new CommodityApplyDetailDTO();
                            temp.setApplyId(applyDetail.getApplyId());
                            temp.setStockNum(applyDetail.getStockNum());
                            temp.setRenderStockNum(detail.getRenderStockNum());
                            temp.setPreStockNum(applyDetail.getPreStockNum());
                            temp.setSkuCode(applyDetail.getSkuCode());
                            temp.setSkuId(applyDetail.getSkuId());
                            temp.setSpecName(applyDetail.getSpecName());
                            for (CommodityApplyMaterialDTO material : materials) {
                                if (detail.getSkuId().equals(material.getSkuId())) {
                                    temp.setMaterialNum(material.getMaterialNum());
                                    break;
                                }
                            }
                            tempList.add(temp);
                            break;
                        }
                    }
                }
                render.setDetails(tempList);
            }
            applyDTO.setRenderResultList(renders);
        }
        return applyDTO;
    }

    @Override
    public int render(CommodityRenderVo renderVo, String createBy) {

        Long applyId = renderVo.getApplyId();
        List<CommodityRenderDTO> list = renderVo.getList();
        if (list.size() > 0) {
            // 1.物料库存占用
            for (CommodityRenderDTO obj : list) {
                obj.setCreateBy(createBy);
                List<CommodityApplyMaterialDTO> materials = commodityApplyMapper.findMaterials(obj.getApplyId(), obj.getSkuId());
                for (CommodityApplyMaterialDTO material : materials) {
                    int renderMaterialNum = obj.getRenderStockNum() * material.getMaterialNum();
                    StockOccupy4CommodityDTO occupyDTO = new StockOccupy4CommodityDTO();
                    occupyDTO.setMaterialId(material.getMaterialId());
                    occupyDTO.setWarehouseId(obj.getWarehouseId());
                    occupyDTO.setOccupyNum(renderMaterialNum);
                    occupyDTO.setUpdateBy(createBy);
                    occupyDTO.setLogTypes((byte) 1);
                    occupyDTO.setChangeReasonId(applyId);
                    stockService.occupyStock(occupyDTO);
                }
            }
            // 2.存入本地
            commodityApplyMapper.render(list);
        }
        // 3.生成预售库存返回参数
        CommodityApplyDTO applyDTO = commodityApplyMapper.findById(applyId);
        List<CommodityApplyDetailDTO> details = applyDTO.getDetails();
        // 4.调用商品服务传参
        CommodityRenderApplyDTO param = new CommodityRenderApplyDTO();
        param.setCommodityRenderDTOS(list);
        param.setDetails(details);
        param.setSpuId(applyDTO.getSpuId());
        inventoryApi.supplySubWarehouse(param);

        // 5.更新申请状态
        CommodityApplyDTO applyUpdate = new CommodityApplyDTO();
        applyUpdate.setId(applyId);
        int rows = 0;
        if (renderVo.getType() == 0) {
            applyUpdate.setStatus(1);
            rows = commodityApplyMapper.update(applyUpdate);
        } else {
            applyUpdate.setPreStatus(1);
            rows = commodityApplyMapper.updatePre(applyUpdate);
        }
        return rows;
    }

}
